package com.zmn.oms.dubbo.impl.zmn.master.order;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.OmsOperator;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.dto.common.work.BaseRequestDIO;
import com.zmn.oms.dubbo.dto.common.work.ModifyProductDIO;
import com.zmn.oms.dubbo.dto.common.work.WorkDetailDRO;
import com.zmn.oms.dubbo.interfaces.zmn.master.order.MasterWorkModifyRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.work.masterwork.ModifyProductDTO;
import com.zmn.oms.model.dto.work.masterwork.WorkDetailDTO;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkModifyProductBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author sunlife
 * @date:2022/5/30 下午8:46
 * description: 工程师工单修改类
 */
@Slf4j
@DubboService(loadbalance = "roundrobin", timeout = 20000, retries = 2, version = DubboConsts.INTERFACE_VERSION)
public class MasterWorkModifyRemoteServiceImpl implements MasterWorkModifyRemoteService {
    private static final String TAG = "啄木鸟-普通工单-工程师工单编辑dubbo接口";

    @Autowired
    private OrderWorkModifyProductBService orderWorkModifyProductBService;

    @Override
    public ResponseDTO modifySaleProduct(ModifyProductDIO modifyProductDIO) {
        log.info("[{}] 修改订单新机销售产品：ModifyProductDIO={}", TAG, JSON.toJSONString(modifyProductDIO));

        try {
            ModifyProductDTO modifyProductDTO = BeanMapper.map(modifyProductDIO, ModifyProductDTO.class);
            this.setLogParam(modifyProductDIO, modifyProductDTO);

            // 修改产品，有可能修改订单计价器类型，清空，由后台判断是否修改
            modifyProductDTO.setServItemType(null);

            orderWorkModifyProductBService.updateOrderSaleProductByMaster(modifyProductDTO);

            return ResponseDTO.success();
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("修改新机销售产品失败", e);
            return ResponseDTO.fail("修改产品失败，请稍后重试");
        }
    }


    // 设置日志参数
    protected void setLogParam(BaseRequestDIO dio, OmsOperator dto) {
        dto.setPlat(dio.getPlat());
        dto.setAppPlat(dio.getAppPlat());
        dto.setVersion(dio.getVersion());
        dto.setDisplayVersion(dio.getDisplayVersion());
        dto.setOrderId(dio.getOrderId());
        dto.setWorkId(dio.getWorkId());
        dto.setOperator(dio.getMasterName());
        dto.setOperatorId((long)dio.getMasterId());
        dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);
        dto.setLongitude(dio.getLongitude());
        dto.setLatitude(dio.getLatitude());
    }
}
